﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml.Serialization;

namespace SourceMaster.Util
{
    [XmlRoot("Configuration")]
    public class Configuration
    {
        [XmlArrayItem("Repository")]
        [XmlArray("RecentRepositories")]
        public List<RepositoryConfiguration> RecentRepositories { get; set; }

        [XmlArray("OpenedRepositories")]
        public List<RepositoryConfiguration> OpenedRepositories { get; set; }

        public static Configuration LoadConfiguration()
        {
            if(File.Exists("./Configuration.xml"))
            {
                using (var fileStream = new FileStream("./Configuration", FileMode.Open))
                {
                    return (Configuration)new XmlSerializer(typeof(Configuration)).Deserialize(fileStream);
                }
            }
            var configuration = new Configuration();
            configuration.Save();

            return configuration;
        }

        public void Save()
        {
            using (var fileStream = new FileStream("./Configuration.xml", FileMode.Create))
            {
                new XmlSerializer(typeof(Configuration)).Serialize(fileStream, this);
            }
        }

        public Configuration()
        {
            RecentRepositories = new List<RepositoryConfiguration>();
            OpenedRepositories = new List<RepositoryConfiguration>();
        }
    }

    public class RepositoryConfiguration
    {
        [XmlAttribute("Name")]
        public string Name { get; set; }

        [XmlAttribute("RepositoryFullPath")]
        public string RepositoryFullPath { get; set; }
    }
}
